﻿using ProjectFootball.MVC.Areas.VenueArea.Models;
using ProjectFootball.MVC.Foundation.Repositories;
using ProjectFootball.MVC.Foundation.ResourceLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;

namespace ProjectFootball.MVC.Models.EntityFramework.Entities
{
    public static class _VenueExtensions
    {
        public static VenueDataModel GetDataModel(this Venue venue)
        {
            return new VenueDataModel()
            {
                VenueGuid = venue.Pk,
                ApprovalGuid = venue.ApprovalGuid,
                VenueName = venue.VenueName,
                Address = venue.Address,
                CountryGuid = venue.CountryGuid,
                Capacity = venue.Capacity
            };
        }

        public static IEnumerable<VenueSearchResultViewModel> Search(this IQueryable<Venue> venues, IPrincipal user, SearchType searchType)
        {
            var query = venues.Where(v => !v.IsMarkedForDeletion).AsEnumerable().Where(c => c.CanView(user)).OrderBy(c => c.VenueName);

            foreach (Venue venue in query)
                yield return venue.GetSearchResultViewModel(user, searchType);
        }

        public static VenueSearchResultViewModel GetSearchResultViewModel(this Venue venue, IPrincipal user, SearchType searchType)
        {
            return new VenueSearchResultViewModel()
            {
                Venue = venue,
                IsVisible = venue.CanView(user),
                IsEditVisible = searchType == SearchType.Details ? venue.CanView(user) : false,
                IsDeleteVisible = (searchType == SearchType.Details || (searchType == SearchType.RecordForApproval && !venue.IsApproved)) ? venue.CanDelete(user) : false,
                IsViewApprovalsVisible = (searchType == SearchType.Details || searchType == SearchType.PendingApprovals) ? venue.CanApprove(user) : false,
                IsConfirmApprovalVisible = searchType == SearchType.RecordForApproval ? venue.CanApprove(user) : false
            };
        }

        public static void Approve(this Venue venue, Venue previousApproved)
        {
            previousApproved.VenueName = venue.VenueName;
            previousApproved.Address = venue.Address;
            previousApproved.CountryGuid = venue.CountryGuid;
            previousApproved.Capacity = venue.Capacity;
            previousApproved.AuditUserId = venue.AuditUserId;
            previousApproved.AuditTimestamp = venue.AuditTimestamp;
        }

        public static void Edit(this Venue venue, VenueDataModel venueDataModel, IPrincipal user)
        {
            venue.ApprovalGuid = venueDataModel.ApprovalGuid;
            venue.VenueName = venueDataModel.VenueName;
            venue.Address = venueDataModel.Address;
            venue.CountryGuid = venueDataModel.CountryGuid;
            venue.Capacity = venueDataModel.Capacity;
            venue.IsApproved = venue.IsApproved ? user.IsApprover() : false;
            venue.AuditUserId = user.GetUserId();
            venue.AuditTimestamp = DateTime.Now;
        }
    }
}